/**
 * Created by yzl on 2017/9/18.
 */
!
    (function ($) {
        var shareFn = function (options) {
            this.config = {
                Flag: true,
                Event: 'click',
                Content: 'content',
                Title: '分享至',
                shareFrom: '',
                sinaWeiboAppKey: '',
            };
            this.options = $.extend(this.config, options);
            this.shareHtml = '';
        };

        shareFn.prototype = {
            init: function () {
                var that = this;
                if (!that.options.Flag) return;
                that.shareHtml = '';
                that.shareHtml += '<div id="oShare" class="oShare__wrap"><section class="sTitle"><strong class="fs14">' + that.options.Title + '</strong></section><ul class="clearfix">';
                that.shareHtml += '<li title="分享到微信/朋友圈"><a href="javascript:void(0)" name="share1" class="share1 c-333"><i class="fa fa-weixin"></i><br><strong class="fs14">微信/朋友圈</strong><span class="js-apcode__wrap"><span id="qrCode"></span><small class="fs12 c-999">扫一扫分享</small></span></a><span></span></li>';
                that.shareHtml += '<li title="分享到新浪微博"><a href="javascript:void(0)" name="share2" class="share2 c-333"><i class="fa fa-weibo"></i><br><strong class="fs14">新浪微博</strong></a><span></span></li>';
                that.shareHtml += '<li title="分享到QQ空间"><a href="javascript:void(0)" name="share3" class="share3 c-333"><i class="fa fa-qq"></i><br><strong class="fs14">QQ空间</strong></a><span></span></li>';
                that.shareHtml += '</ul></div>';
                that.start();
            },
            start: function () {
                var that = this;
                $('.oShareBtn').on('click', function () {
                    that.options.pics = $(this).data('share');
                    that.options.mobile = $(this).data('mobile');
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 1,
                        area: ['420px', '157px'],
                        shadeClose: true,
                        skin: 'lay_ppComment__wrap',
                        content: that.shareHtml,
                        btn: 0,
                        success: function (layero, index) {
                            var _obj = layero.find('#oShare').attr('id');
                            var _canvas = layero.find('#qrCode').attr('id');
                            new QRCode(document.getElementById(_canvas), {
                                text: that.getShareUrl(),
                                width: 100,
                                height: 100,
                                correctLevel: QRCode.CorrectLevel.H
                            });
                            that.calculate(_obj);
                            layero.find('#qrCode').attr('title','');
                        }
                    })
                })
            },
            calculate: function (obj) {
                //debugger;
                var that = this;
                var pics = that.options.pics;
                var share_from = encodeURIComponent(document.title)+"_"+that.options.shareFrom;
                var share_title = encodeURIComponent(document.title);
                $('#' + obj).on('click', 'li', function () {
                    var _this = $(this),
                        _li = _this.children('a').attr('name'),
                        share_url = encodeURIComponent(getSF());
                    switch (_li) {
                        case 'share1' : // 分享至微信
                            break;
                        case 'share2' : // 分享至新浪微博
                            var param = {
                                url: share_url,
                                appkey: that.options.sinaWeiboAppKey,
                                title: share_title,
                                rnd: new Date().valueOf()
                            };
                            var temp = [];
                            for (var p in param) {
                                temp.push(p + '=' + encodeURIComponent(param[p] || ''))
                            }
                            window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
                            break;
                        case 'share3' : // 分享至QQ空间
                            var p = {
                                url: share_url,
                                showcount: '0', /*是否显示分享总数,显示：'1'，不显示：'0' */
                                desc: share_from, /*默认分享理由(可选)*/
                                title: share_title, /*分享标题(可选)*/
                                pics: pics, /*分享图片的路径(可选)*/
                                style: '203',
                                width: 98,
                                height: 22
                            };
                            var s = [];
                            for (var i in p) {
                                s.push(i + '=' + p[i]);
                            }
                            window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?' + s.join('&'));
                            break;
                    }
                })
            },
            getShareUrl: function () {
                //处理分享的链接
                var share_url = this.options.mobile || location.href;
                if (SHARE_KEY && $.trim(SHARE_KEY) != '' && typeof  SHARE_KEY != 'undefined') {
                    if (share_url.indexOf("shareKey=") != -1) {
                        share_url = share_url.replace(/shareKey=[a-z0-9]*[\&]/g, '');
                    }
                    if (share_url.indexOf("?") == -1) {
                        share_url = share_url + "?" + "shareKey=" + SHARE_KEY;
                    } else {
                        share_url = share_url + "&" + "shareKey=" + SHARE_KEY;
                    }
                }
                return share_url;
            }
        },getSF = function () {
            //处理分享的链接
            var share_url = location.href;
            if (SHARE_KEY && $.trim(SHARE_KEY) != '' && typeof  SHARE_KEY != 'undefined') {
                if (share_url.indexOf("shareKey=") != -1) {
                    share_url = share_url.replace(/shareKey=[a-z0-9]*[\&]/g, '');
                }
            }
            return share_url;
        };
        var opShare = new shareFn({
            Flag: true,
            Title: '分享至',
            shareFrom: siteTile,
            sinaWeiboAppKey: '1451504083'
        });
        opShare.init();
    })(jQuery);
